# -*- coding:utf-8 –*-
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
import time
from django.http import JsonResponse
import json
from aliyun.log import LogClient
from aliyun.log.getlogsrequest import GetLogsRequest


def cls_query(SecretId, SecretKey, cls_Region, cls_TopicId, From, To, query):
    from tencentcloud.cls.v20201016 import cls_client, models
    try:
        cred = credential.Credential(SecretId, SecretKey)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cls.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cls_client.ClsClient(cred, cls_Region, clientProfile)
        req = models.SearchLogRequest()
        params = {
            "TopicId": cls_TopicId,
            "From": From * 1000,
            "To": To * 1000,  # 1660720162000
            "Query": query
        }
        req.from_json_string(json.dumps(params))
        resp = client.SearchLog(req)
        return True, json.loads(resp.to_json_string())
    except TencentCloudSDKException as err:
        return False, err


from server.models import *

NAME_LIST_REQUEST_COUNT_TREND_7D = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1d', '%m-%d-%H', '0') as time,count(*) as count group by time order by time"
NAME_LIST_REQUEST_COUNT_TREND_24H = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1h','%m-%d-%H', '0') as time,count(*) as count group by time order by time"
NAME_LIST_REQUEST_COUNT_TREND_1H = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'5m', '%H:%i:%s', '0') as time,count(*) as count group by time order by time"
DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_7D = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1d', '%m-%d-%H', '0') as time,count(*) as count group by time order by time"
DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_24H = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1h','%m-%d-%H', '0') as time,count(*) as count group by time order by time"
DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_1H = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'5m', '%H:%i:%s', '0') as time,count(*) as count group by time order by time"

NAME_LIST_IP_COUNT_TREND_7D = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1d', '%m-%d-%H', '0') as time,count(DISTINCT src_ip) as count group by time order by time"
NAME_LIST_IP_COUNT_TREND_24H = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1h','%m-%d-%H', '0') as time,count(DISTINCT src_ip) as count group by time order by time"
NAME_LIST_IP_COUNT_TREND_1H = "*  and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'5m', '%H:%i:%s', '0') as time,count(DISTINCT src_ip) as count group by time order by time"
DOMAIN_NAME_LIST_IP_COUNT_TREND_7D = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1d', '%m-%d-%H', '0') as time,count(DISTINCT src_ip) as count group by time order by time"
DOMAIN_NAME_LIST_IP_COUNT_TREND_24H = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'1h','%m-%d-%H', '0') as time,count(DISTINCT src_ip) as count group by time order by time"
DOMAIN_NAME_LIST_IP_COUNT_TREND_1H = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select time_series(cast(request_time as timestamp)- interval 8 hour,'5m', '%H:%i:%s', '0') as time,count(DISTINCT src_ip) as count group by time order by time"

NAME_LIST_REQUEST_COUNT_TOTLE = "*  and (waf_module: global_name_list or waf_module: name_list) | select count(*) as count"
DOMAIN_NAME_LIST_REQUEST_COUNT_TOTLE = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select count(*) as count"

NAME_LIST_REQUEST_IP_TOTLE = "*  and (waf_module: global_name_list or waf_module: name_list) | select count(DISTINCT src_ip) as count"
DOMAIN_NAME_LIST_REQUEST_IP_TOTLE = "* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list) | select count(DISTINCT src_ip) as count"

NAME_LIST_ATT_TYPE_TOP10 = '* and (waf_module: global_name_list or waf_module: name_list)  | select waf_policy as waf_policy,count(*) as count group by waf_policy order by count desc limit 10'
DOMAIN_NAME_LIST_ATT_TYPE_TOP10 = '* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list)  | select waf_policy as waf_policy,count(*) as count group by waf_policy order by count desc limit 10'

NAME_LIST_ATT_IP_TOP10 = '* and (waf_module: global_name_list or waf_module: name_list)  | select src_ip as src_ip,count(*) as count group by src_ip order by count desc limit 10'
DOMAIN_NAME_LIST_ATT_IP_TOP10 = '* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list)  | select src_ip as src_ip,count(*) as count group by src_ip order by count desc limit 10'

NAME_LIST_ATT_URI_TOP10 = '* and (waf_module: global_name_list or waf_module: name_list)  | select uri as uri,count(*) as count group by uri order by count desc limit 10'
DOMAIN_NAME_LIST_ATT_URI_TOP10 = '* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list)  | select uri as uri,count(*) as count group by uri order by count desc limit 10'

NAME_LIST_ATT_IP_COUNTRY_TOP10 = '* and (waf_module: global_name_list or waf_module: name_list)  | select ip_to_country(src_ip) as country,count(*) as count group by country order by count desc limit 10'
DOMAIN_NAME_LIST_ATT_IP_COUNTRY_TOP10 = '* and host: \"{}\" and (waf_module: global_name_list or waf_module: name_list)  | select ip_to_country(src_ip) as country,count(*) as count group by country order by count desc limit 10'


def cls_report_name_list_request_count_trend(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_7D.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_24H.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TREND_1H.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_REQUEST_COUNT_TREND_7D
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_REQUEST_COUNT_TREND_24H
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_REQUEST_COUNT_TREND_1H
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_ip_count_trend(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_IP_COUNT_TREND_7D.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_IP_COUNT_TREND_24H.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_IP_COUNT_TREND_1H.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_IP_COUNT_TREND_7D
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_IP_COUNT_TREND_24H
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_IP_COUNT_TREND_1H
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_request_count_totle(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TOTLE.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TOTLE.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_REQUEST_COUNT_TOTLE.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_REQUEST_COUNT_TOTLE
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_REQUEST_COUNT_TOTLE
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_REQUEST_COUNT_TOTLE
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_request_ip_totle(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_REQUEST_IP_TOTLE.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_REQUEST_IP_TOTLE.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_REQUEST_IP_TOTLE.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_REQUEST_IP_TOTLE
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_REQUEST_IP_TOTLE
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_REQUEST_IP_TOTLE
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_att_type_top10(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_ATT_TYPE_TOP10.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_ATT_TYPE_TOP10.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_ATT_TYPE_TOP10.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_ATT_TYPE_TOP10
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_ATT_TYPE_TOP10
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_ATT_TYPE_TOP10
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_att_ip_top10(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_TOP10.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_TOP10.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_TOP10.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_ATT_IP_TOP10
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_ATT_IP_TOP10
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_ATT_IP_TOP10
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_att_uri_top10(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_ATT_URI_TOP10.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_ATT_URI_TOP10.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_ATT_URI_TOP10.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_ATT_URI_TOP10
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_ATT_URI_TOP10
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_ATT_URI_TOP10
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)


def cls_report_name_list_att_ip_country_top10(request):
    return_result = {}
    try:
        user_id = request.session['user_id']
        json_data = json.loads(request.body)
        time_zone = json_data['time_zone']
        try:
            domain = json_data['domain']
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_COUNTRY_TOP10.format(domain)
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_COUNTRY_TOP10.format(domain)
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = DOMAIN_NAME_LIST_ATT_IP_COUNTRY_TOP10.format(domain)
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        except:
            if time_zone == "7day":
                from_time = int(time.time() - 604800)
                req_sql = NAME_LIST_ATT_IP_COUNTRY_TOP10
            elif time_zone == "24hour":
                from_time = int(time.time() - 86400)
                req_sql = NAME_LIST_ATT_IP_COUNTRY_TOP10
            elif time_zone == "1hour":
                from_time = int(time.time() - 3600)
                req_sql = NAME_LIST_ATT_IP_COUNTRY_TOP10
            else:
                return_result['result'] = False
                return_result['message'] = "time_zone error"
                return JsonResponse(return_result, safe=False)
        sys_report_conf_result = sys_report_conf.objects.get(user_id=user_id)
        to_time = int(time.time())
        cls_SecretId = sys_report_conf_result.cls_SecretId
        cls_SecretKey = sys_report_conf_result.cls_SecretKey
        cls_Region = sys_report_conf_result.cls_Region
        cls_TopicId = sys_report_conf_result.cls_TopicId
        cls_result, cls_message = cls_query(cls_SecretId, cls_SecretKey, cls_Region, cls_TopicId, from_time, to_time,
                                            req_sql)
        if cls_result == True:
            return_result['result'] = True
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
        else:
            return_result['result'] = False
            return_result['message'] = cls_message
            return JsonResponse(return_result, safe=False)
    except Exception, e:
        return_result['result'] = False
        return_result['message'] = str(e)
        return_result['errCode'] = 400
        return JsonResponse(return_result, safe=False)
